/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.pub.entity.AdMake;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.Myrelease;
import com.thinkgem.jeesite.modules.pub.service.MyreleaseService;

/**
 * 我要发布Controller
 * @author 开发者
 * @version 2016-11-14
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/myrelease")
public class MyreleaseController extends BaseController {

	@Autowired
	private MyreleaseService myreleaseService;
	
	@ModelAttribute
	public Myrelease get(@RequestParam(required=false) String id) {
		Myrelease entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = myreleaseService.get(id);
		}
		if (entity == null){
			entity = new Myrelease();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:myrelease:view")
	@RequestMapping(value = {"list", ""})
	public String list(Myrelease myrelease, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Myrelease> page = myreleaseService.findPage(new Page<Myrelease>(request, response), myrelease);
//		myrelease.setDelFlag("0");
		model.addAttribute("page", page);
		return "modules/pub/myreleaseList";
	}

	@RequiresPermissions("pub:myrelease:view")
	@RequestMapping(value = "form")
	public String form(Myrelease myrelease, Model model) {
		model.addAttribute("myrelease", myrelease);
		return "modules/pub/myreleaseForm";
	}

	@RequiresPermissions("pub:myrelease:edit")
	@RequestMapping(value = "save")
	public String save(Myrelease myrelease, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, myrelease)){
			return form(myrelease, model);
		}
		if(myrelease.getDescribes() != null){
			myrelease.setDescribes(StringEscapeUtils.unescapeHtml4(
					myrelease.getDescribes()));
		}
		myreleaseService.save(myrelease);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/pub/myrelease/?repage";
	}
	
	@RequiresPermissions("pub:myrelease:edit")
	@RequestMapping(value = "delete")
	public String delete(Myrelease myrelease, @RequestParam(required = false) Boolean isRe,RedirectAttributes redirectAttributes) {
		if(!UserUtils.getSubject().isPermitted("pub:myrelease:audit")){
			addMessage(redirectAttributes,"您没有删除或发布权限");
		}
		myreleaseService.delete(myrelease,isRe);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/pub/myrelease/?repage";
	}


}